package com.wang.project.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.project.common.ErrorCode;
import com.wang.project.exception.BusinessException;
import com.wang.project.model.dto.approval.ApprovalQueryRequest;
import com.wang.project.model.dto.approval.ApprovalReSubmitRequest;
import com.wang.project.model.dto.approval.ApprovalSubmitRequest;
import com.wang.project.model.entity.Approval;
import com.wang.project.model.entity.User;
import com.wang.project.model.enums.ApprovalStatusEnum;
import com.wang.project.model.vo.ApprovalVO;
import com.wang.project.service.ApprovalService;
import com.wang.project.mapper.ApprovalMapper;
import com.wang.project.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 胡歌的腿毛
 * @description 针对表【approval】的数据库操作Service实现
 * @createDate 2023-09-29 22:36:15
 */
@Service
@Slf4j
public class ApprovalServiceImpl extends ServiceImpl<ApprovalMapper, Approval>
        implements ApprovalService {

    @Resource
    private ApprovalMapper approvalMapper;
    @Resource
    private UserService userService;

    /**
     * 获取审核列表
     *
     * @param approvalQueryRequest
     * @param request
     * @return
     */
    @Override
    public List<ApprovalVO> getApprovalList(ApprovalQueryRequest approvalQueryRequest, HttpServletRequest request) {
        if (approvalQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        log.info("ApprovalServiceImpl approvalQueryRequest:{}", JSONUtil.toJsonStr(approvalQueryRequest));
        if (userService.isAdmin(request)) {
            //管理员，返回全部
            List<ApprovalVO> approvalVOList = approvalMapper.getApprovalList(approvalQueryRequest);
            return approvalVOList;
        }
        //只返回当前用户的
        User loginUser = userService.getLoginUser(request);
        List<ApprovalVO> userApprovalVOList = approvalMapper.getUserApprovalList(approvalQueryRequest, loginUser.getId());
        return userApprovalVOList;
    }

    /**
     * 管理员进行审核
     *
     * @param approvalSubmitRequest
     * @param request
     * @return
     */
    @Override
    public Boolean approvalSubmit(ApprovalSubmitRequest approvalSubmitRequest, HttpServletRequest request) {
        if (approvalSubmitRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        log.info("ApprovalServiceImpl approvalSubmit approvalQueryRequest:{}", JSONUtil.toJsonStr(approvalSubmitRequest));
        Approval approval = new Approval();
        BeanUtils.copyProperties(approvalSubmitRequest, approval);
        return this.updateById(approval);
    }

    /**
     * 用户重新提交审核
     *
     * @param approvalReSubmitRequest
     * @param request
     * @return
     */
    @Override
    public Boolean approvalReSubmit(ApprovalReSubmitRequest approvalReSubmitRequest, HttpServletRequest request) {
        if (approvalReSubmitRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        log.info("ApprovalServiceImpl approvalReSubmit approvalReSubmitRequest:{}", JSONUtil.toJsonStr(approvalReSubmitRequest));
        Approval approval = new Approval();
        BeanUtils.copyProperties(approvalReSubmitRequest, approval);
        approval.setApprovalStatus(ApprovalStatusEnum.APPROVALING.getValue());
        return this.updateById(approval);
    }

}




